/*
 * Ingenieria de Software II - 2C2007
 * Agosto/2007
 * 
 * Ejercicio 18 - JSP Modelo 2
 *  
 */

package web;

import javax.servlet.http.HttpServlet;

import javax.servlet.ServletException;
import javax.servlet.ServletConfig;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import utils.ZaraException;
//import javax.servlet.http.HttpSession;

//import exceptions.PasswordIncorrectoException;
//import exceptions.UsuarioIncorrectoException;

import entidadesVO.*;
import facade.Facade;
import model.*;

public class SolDist extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6711596082768458467L;

	public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
	
	private Facade facade; 

	public SolDist() {
		super();
		// TODO Auto-generated constructor stub
	}

	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		BusinessDelegate businessDelegate = new BusinessDelegate();
		facade = businessDelegate.getFacade();
		
	}

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException,
		IOException {
		
		String nombre_cd = req.getParameter("cd");
        
        try {
			SolDistVO solDist = facade.getSolDist(nombre_cd);
			resp.setContentType("text/xml");
	        resp.setHeader("Cache-Control", "no-cache");
	        
	        Object[] articulosSolDist = solDist.getArticulosSolDist().toArray();
	        
	        for (int i=0; i< articulosSolDist.length; i++){
	        	ArticulosSolDistVO articuloSolDist = (ArticulosSolDistVO)articulosSolDist[i]; 
	        	resp.getWriter().write("<articulo>");
	        	resp.getWriter().write("<descripcion>"+articuloSolDist.getArticulo().getDescripcion()+"</descripcion>");
	        	resp.getWriter().write("<cantidad>"+articuloSolDist.getCantidad()+"</cantidad>");
	        	resp.getWriter().write("</articulo>");
	        }
	        
		} catch (ZaraException e) {
			resp.setContentType("text/xml");
	        resp.setHeader("Cache-Control", "no-cache");
	        resp.getWriter().write("<er>No hay pedidos para el centro de distribucion seleccionado!</er>");
		}
	}
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException,
	IOException {
		//Generacion de XML
	}
}
	
